package com.palfund.animationandanimator;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private AnimationDrawable mAnimationDrawable_xml;
    private AnimationDrawable mAnimationDrawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView_xml = (ImageView) findViewById(R.id.imageView_xml);
        mAnimationDrawable_xml = (AnimationDrawable) imageView_xml.getBackground();
        //Java
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        mAnimationDrawable = new AnimationDrawable();
        for (int i = 0; i < 11; i++) {
            int id = getResources().getIdentifier("girl_" + (i + 1), "drawable", getPackageName());

            Drawable drawable = getResources().getDrawable(id);
            mAnimationDrawable.addFrame(drawable, 200);
        }
        mAnimationDrawable.setOneShot(false);
        imageView.setBackgroundDrawable(mAnimationDrawable);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(300);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.addPauseListener(new Animator.AnimatorPauseListener() {
            @Override
            public void onAnimationPause(Animator animation) {

            }

            @Override
            public void onAnimationResume(Animator animation) {

            }
        });
        valueAnimator.start();
    }
    public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_start_xml:
                if (!mAnimationDrawable_xml.isRunning()) {
                    mAnimationDrawable_xml.start();
                }
                break;
            case R.id.btn_stop_xml:
                if (mAnimationDrawable_xml.isRunning()) {
                    mAnimationDrawable_xml.stop();
                }
                break;
            case R.id.btn_start:
                if (!mAnimationDrawable.isRunning()) {
                    mAnimationDrawable.start();
                }
                break;
            case R.id.btn_stop:
                if (mAnimationDrawable.isRunning()) {
                    mAnimationDrawable.stop();
                }
                break;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_tween:
                startActivity(new Intent(this, TweenActivity.class));
                break;
            case R.id.action_property:
                startActivity(new Intent(this, PropertyActivity.class));
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}
